--these are the functions called when a button is pressed

--ctrl is the gui controller object

function pauseButton(ctrl)
    pause = not pause
    for i,v in ipairs(ctrl.buttons) do
		if v.name == "Running " then
            v.name = "Paused "
        elseif v.name == "Paused " then
            v.name = "Running "
        end
	end
end

function toggleDrag(ctrl)
	if dragBox == true then
		dragBox = false
	else
		dragBox = true
	end
	
	for i,v in ipairs(ctrl.buttons) do
		if v.name == "Drag To Place" then
			v.name = "Click To Place"
		elseif v.name == "Click To Place" then
			v.name = "Drag To Place"
		end
	end
end

function clearPath(ctrl)
	local i = 10
	
	while i > 0 do
		--waypoints = {}
		currentPoints = {}
		highlightPath = {}
		successfulPoints = {}
		startPoint = {}
		endPoint = {}
		
		for i,v in ipairs(waypoints) do
			v.prev = {}
			v.next = {}
			v.children = {}
			v = {}
			table.remove(waypoints,i)
		
		end
		i = i - 1
	end
	finalPath = {}
	moonsPath = {}
end

function clearSquares(ctrl)
	terrain = {}
end

function drawPath(ctrl)
	clearPath()
	pathFound = false
	pathRunning = true
	local tempx1 = 0
	local tempy1 = 0
	local tempx2 = 0
	local tempy2 = 0

    
	for i,v in ipairs(ctrl.textboxes) do
		if v.text ~= "" then
			if v.name == "Start X" then
				tempx1 = tonumber(v.text)
			elseif v.name == "Start Y" then
				tempy1 = tonumber(v.text)
			elseif v.name == "End X" then
				tempx2 = tonumber(v.text)
			elseif v.name == "End Y" then
				tempy2 = tonumber(v.text)
            end
		end
	end
	
	
	startTree(tempx1,tempy1,tempx2,tempy2)
end


function explosion1(x,y)

	local p = love.graphics.newParticleSystem(images.explode, 100)
	p:setEmissionRate(100)
	p:setSpeed(300, 400)
	p:setSize(2, 1)
	p:setColor(220, 105, 20, 255, 194, 30, 18, 0)
	p:setPosition(x,y)
	p:setLifetime(1)
	p:setParticleLife(1)
	p:setDirection(0)
	p:setSpread(360)
	
	p:setRadialAcceleration(-20)
	if math.random(2) == 1 then
		p:setRadialAcceleration(-2000)
		p:setTangentialAcceleration(1000)
	end
	
	--p:stop()
	table.insert(particles, p)	
	
	return p
end